/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.farmcode.console.ejb.dao.custom;

import com.farmcode.base.dao.BaseFilter;
import com.farmcode.base.dao.DAOException;
import com.farmcode.console.api.dao.custom.ExtProjectDAO;
import com.farmcode.console.api.dao.impl.BaseProjectDAOImpl;
import com.farmcode.console.api.model.Project;
import java.util.List;
import javax.ejb.Local;
import javax.ejb.Stateless;

/**
 *
 * @author Oscar Ivan Prado
 */
@Local(ExtProjectDAO.class)
@Stateless
public class ExtProjectDAOImpl extends BaseProjectDAOImpl implements ExtProjectDAO{

    @Override
    public com.farmcode.console.api.model.Project getProjectByName(String name) throws DAOException{
        setFilter(new ProjectFilter(name));
        List<Project> projects = find();
        
        return projects.size() > 0 ? (com.farmcode.console.api.model.Project)projects.get(0) : null;
    }

    @Override
    public List<Project> getProjectByUser(String userName) throws DAOException {
        setFilter(new FilterProjectByUserName(userName));
        return find();
    }
    
    private class FilterProjectByUserName extends BaseFilter{

        private String userName;

        public FilterProjectByUserName(String userName) {
            this.userName = userName;
        }
        
        @Override
        public void fill() {
//            setParameter("status", "UPPER(project.status.description) = ", "ACTIVE");
//            setParameter("userName", "UPPER(project.userName) = ", userName.toUpperCase());
        }
        
    }
    
    private class ProjectFilter extends BaseFilter{
        private String name;

        public ProjectFilter(String name) {
            this.name = name;
        }
        
        @Override
        public void fill() {
            setParameter("name", "UPPER(project.name) like ", "%"+name.toUpperCase()+"%");
        }

        /**
         * @return the name
         */
        public String getName() {
            return name;
        }

        /**
         * @param name the name to set
         */
        public void setName(String name) {
            this.name = name;
        }
        
    }

    
}
